<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>参加考试 - AI智能教学培训系统</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" />
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        /* 全局样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4895ef;
            --light: #f8f9fa;
            --dark: #212529;
            --success: #4cc9f0;
            --warning: #f72585;
            --gray: #6c757d;
        }

        body {
            background-color: #f5f7fb;
            color: var(--dark);
        }

        .app-container {
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 260px;
            background: linear-gradient(180deg, var(--primary), var(--secondary));
            color: white;
            box-shadow: 3px 0 10px rgba(0, 0, 0, 0.1);
            transition: all 0.3s;
            z-index: 100;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
        }

        .logo-container {
            padding: 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .logo {
            font-size: 24px;
            font-weight: 700;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-icon {
            margin-right: 10px;
            font-size: 28px;
        }

        .nav-menu {
            padding: 20px 0;
        }

        .nav-item {
            padding: 12px 25px;
            display: flex;
            align-items: center;
            cursor: pointer;
            transition: all 0.3s;
            border-left: 4px solid transparent;
        }

        .nav-item:hover, .nav-item.active {
            background: rgba(255, 255, 255, 0.1);
            border-left: 4px solid var(--accent);
        }

        .nav-icon {
            margin-right: 12px;
            font-size: 18px;
        }

        /* 主内容区样式 */
        .main-content {
            flex: 1;
            margin-left: 260px;
            padding: 30px;
            transition: all 0.3s;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }

        .page-title {
            font-size: 28px;
            font-weight: 600;
            color: var(--dark);
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: var(--accent);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            margin-right: 10px;
        }

        /* 考试列表样式 */
        .exam-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 25px;
        }

        .exam-card {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            transition: all 0.3s;
        }

        .exam-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
        }

        .exam-header {
            position: relative;
            height: 160px;
            background: linear-gradient(135deg, var(--primary), var(--accent));
        }

        .exam-tag {
            position: absolute;
            top: 15px;
            right: 15px;
            background: white;
            color: var(--primary);
            padding: 4px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
        }

        .exam-body {
            padding: 20px;
        }

        .exam-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .exam-details {
            color: var(--gray);
            font-size: 14px;
            margin-bottom: 15px;
        }

        .exam-actions {
            display: flex;
            justify-content: flex-end;
            margin-top: 20px;
        }

        .action-btn {
            padding: 10px 20px;
            border: none;
            background: var(--primary);
            color: white;
            cursor: pointer;
            transition: all 0.3s;
            font-size: 14px;
            border-radius: 6px;
        }

        .action-btn:hover {
            background: var(--secondary);
        }

        /* 空状态样式 */
        .empty-state {
            text-align: center;
            padding: 50px 20px;
            grid-column: 1 / -1;
        }

        .empty-icon {
            font-size: 60px;
            color: #dee2e6;
            margin-bottom: 20px;
        }

        .empty-text {
            font-size: 18px;
            color: var(--gray);
            margin-bottom: 20px;
        }
        /* 侧边栏链接无下划线且不变色 */
        a.nav-item {
            text-decoration: none;
            color: inherit;
        }
        a.nav-item:visited,
        a.nav-item:active,
        a.nav-item:focus {
            color: inherit;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="app-container">
        <!-- 侧边导航 -->
        <div id="sidebar"></div>


        <!-- 主内容区 -->
        <div class="main-content">
            <!-- 顶部导航 -->
            <div class="header">
                <h1 class="page-title">我的考试</h1>
                <div class="user-info">
                    <div class="avatar">张</div>
                    <div>
                        <div class="user-name">张明</div>
                        <div class="user-role">学生</div>
                    </div>
                </div>
            </div>

            <!-- 考试列表 -->
            <div class="exam-list">
                <template v-if="exams.length > 0">
                    <div class="exam-card fade-in" v-for="(exam, index) in exams" :key="index">
                        <div class="exam-header" :style="{background: exam.color}">
                            <div class="exam-tag">
                              {{'已完成'}}
                            </div>
                        </div>
                        <div class="exam-body">
                            <div class="exam-title">{{ exam.title }}</div>
                            <div class="exam-details">
                                题目数量: {{ exam.questions }}<br/>
                                考试时长: {{ exam.duration }} 分钟
                            </div>
                            <div class="exam-actions">
                                <button class="action-btn" @click="viewExamDetail(exam)">
                                  {{ '查看详情' }}
                                </button>
                            </div>
                        </div>
                    </div>
                </template>

                <div class="empty-state" v-else>
                    <i class="el-icon-document-empty empty-icon"></i>
                    <div class="empty-text">暂无可用考试</div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            exams: [] // 改为由API获取
        },
        mounted() {
            fetch('/exams', {
                headers: {
                    Authorization: 'Bearer ' + localStorage.getItem('token')
                }
            })
            .then(res => res.json())
            .then(resp => {
                if (resp.success && Array.isArray(resp.data)) {
                    // 为API数据添加样式属性，保持原有的美观效果
                    this.exams = resp.data.map((exam, index) => ({
                        ...exam,
                        id: exam.id || exam.examId, // 兼容 examId 字段
                        // 添加渐变色背景
                        color: this.getExamColor(index),
                        // 确保状态字段存在
                        status: exam.status || '待考试',
                        // 确保题目数量字段存在
                        questions: exam.questions || 10,
                        // 确保时长字段存在
                        duration: exam.duration || 60
                    }));
                } else {
                    console.error('获取考试数据失败:', resp.message);
                    this.exams = [];
                }
            })
            .catch(error => {
                console.error('请求考试数据出错:', error);
                this.exams = [];
            });
        },
        methods: {
            // 根据索引生成考试颜色
            getExamColor(index) {
                const colors = [
                    'linear-gradient(135deg, #4361ee, #4cc9f0)',
                    'linear-gradient(135deg, #2ec4b6, #e71d36)',
                    'linear-gradient(135deg, #f72585, #4895ef)',
                    'linear-gradient(135deg, #f8961e, #577590)'
                ];
                return colors[index % colors.length];
            },
            startExam(exam) {
                this.$message(`正在开始考试: ${exam.title}`);
                // 实际应用中跳转到考试页面
                window.location.href = `exam-detail.html?examId=${exam.id}`;
            },
            viewExamDetail(exam) {
                // 跳转到考试详情页，静态演示用id=demo
                window.location.href = `exam-detail.html?examId=${exam.id}`;
            }
        }
    });
</script>
<script>
  fetch('sidebar.html').then(r=>r.text()).then(html=>{document.getElementById('sidebar').innerHTML=html;});
</script>
</body>
</html>
